//=======================================================
// 作者：KingSley
// 描述：通过DOTween实现弹簧动画效果
//=======================================================
using UnityEngine;
using System.Collections;
using DG.Tweening;

namespace Babybus.Uno
{
    public class DOTweenTest2Spring : MonoBehaviour
    {
        // 弹簧7个组件的位置坐标变换记录
        private Vector3 Spring1Vector = new Vector3(0, 0, 0);
        private Vector3 Spring2Vector = new Vector3(0, 1, 0);
        private Vector3 Spring3Vector = new Vector3(0, 2, 0);
        private Vector3 Spring4Vector = new Vector3(0, 3, 0);
        private Vector3 Spring5Vector = new Vector3(0, 4, 0);
        private Vector3 Spring6Vector = new Vector3(0, 5, 0);
        private Vector3 Spring7Vector = new Vector3(0, 6, 0);

        private bool isHide = false; // 用于判断弹簧是否已经处于压缩状态

        /// <summary>
        /// 弹簧动画按钮点击函数，对弹簧各组件的坐标位置进行修改
        /// </summary>
        private void Start()
        {
            DOTween.To(() => Spring1Vector, x => Spring1Vector = x, new Vector3(0, 0, 0), 2);
            DOTween.To(() => Spring2Vector, x => Spring2Vector = x, new Vector3(0, 0.1f, 0), 2);
            DOTween.To(() => Spring3Vector, x => Spring3Vector = x, new Vector3(0, 1.2f, 0), 2);
            DOTween.To(() => Spring4Vector, x => Spring4Vector = x, new Vector3(0, 1.3f, 0), 2);
            DOTween.To(() => Spring5Vector, x => Spring5Vector = x, new Vector3(0, 2.4f, 0), 2);
            DOTween.To(() => Spring6Vector, x => Spring6Vector = x, new Vector3(0, 2.5f, 0), 2);
            DOTween.To(() => Spring7Vector, x => Spring7Vector = x, new Vector3(0, 3.6f, 0), 2).OnComplete(OnSpringTurn1);
        }

        private void Update()
        {
            // 实时更新弹簧组件的相对位置坐标信息
            GameObject.Find("Spring1").transform.localPosition = Spring1Vector;
            GameObject.Find("Spring2").transform.localPosition = Spring2Vector;
            GameObject.Find("Spring3").transform.localPosition = Spring3Vector;
            GameObject.Find("Spring4").transform.localPosition = Spring4Vector;
            GameObject.Find("Spring5").transform.localPosition = Spring5Vector;
            GameObject.Find("Spring6").transform.localPosition = Spring6Vector;
            GameObject.Find("Spring7").transform.localPosition = Spring7Vector;
        }

        private void OnSpringTurn1()
        {
            DOTween.To(() => Spring1Vector, x => Spring1Vector = x, new Vector3(0, 0, 0), 0.3f);
            DOTween.To(() => Spring2Vector, x => Spring2Vector = x, new Vector3(0, 1.2f, 0), 0.3f);
            DOTween.To(() => Spring3Vector, x => Spring3Vector = x, new Vector3(0, 2.2f, 0), 0.3f);
            DOTween.To(() => Spring4Vector, x => Spring4Vector = x, new Vector3(0, 3.5f, 0), 0.3f);
            DOTween.To(() => Spring5Vector, x => Spring5Vector = x, new Vector3(0, 4.5f, 0), 0.3f);
            DOTween.To(() => Spring6Vector, x => Spring6Vector = x, new Vector3(0, 5.8f, 0), 0.3f);
            DOTween.To(() => Spring7Vector, x => Spring7Vector = x, new Vector3(0, 6.8f, 0), 0.3f).OnComplete(OnSpringTurn2);
        }

        private void OnSpringTurn2()
        {
            DOTween.To(() => Spring1Vector, x => Spring1Vector = x, new Vector3(0, 0, 0), 0.5f);
            DOTween.To(() => Spring2Vector, x => Spring2Vector = x, new Vector3(0, 0.7f, 0), 0.5f);
            DOTween.To(() => Spring3Vector, x => Spring3Vector = x, new Vector3(0, 1.7f, 0), 0.5f);
            DOTween.To(() => Spring4Vector, x => Spring4Vector = x, new Vector3(0, 2.5f, 0), 0.5f);
            DOTween.To(() => Spring5Vector, x => Spring5Vector = x, new Vector3(0, 3.5f, 0), 0.5f);
            DOTween.To(() => Spring6Vector, x => Spring6Vector = x, new Vector3(0, 4.3f, 0), 0.5f);
            DOTween.To(() => Spring7Vector, x => Spring7Vector = x, new Vector3(0, 5.3f, 0), 0.5f).OnComplete(OnSpringTurn3);
        }

        private void OnSpringTurn3()
        {
            DOTween.To(() => Spring1Vector, x => Spring1Vector = x, new Vector3(0, 0, 0), 0.5f);
            DOTween.To(() => Spring2Vector, x => Spring2Vector = x, new Vector3(0, 1.2f, 0), 0.5f);
            DOTween.To(() => Spring3Vector, x => Spring3Vector = x, new Vector3(0, 2.2f, 0), 0.5f);
            DOTween.To(() => Spring4Vector, x => Spring4Vector = x, new Vector3(0, 3.4f, 0), 0.5f);
            DOTween.To(() => Spring5Vector, x => Spring5Vector = x, new Vector3(0, 4.4f, 0), 0.5f);
            DOTween.To(() => Spring6Vector, x => Spring6Vector = x, new Vector3(0, 5.5f, 0), 0.5f);
            DOTween.To(() => Spring7Vector, x => Spring7Vector = x, new Vector3(0, 6.5f, 0), 0.5f).OnComplete(OnSpringTurn4);
        }

        private void OnSpringTurn4()
        {
            DOTween.To(() => Spring1Vector, x => Spring1Vector = x, new Vector3(0, 0, 0), 0.7f);
            DOTween.To(() => Spring2Vector, x => Spring2Vector = x, new Vector3(0, 0.9f, 0), 0.7f);
            DOTween.To(() => Spring3Vector, x => Spring3Vector = x, new Vector3(0, 1.9f, 0), 0.7f);
            DOTween.To(() => Spring4Vector, x => Spring4Vector = x, new Vector3(0, 2.8f, 0), 0.7f);
            DOTween.To(() => Spring5Vector, x => Spring5Vector = x, new Vector3(0, 3.8f, 0), 0.7f);
            DOTween.To(() => Spring6Vector, x => Spring6Vector = x, new Vector3(0, 4.6f, 0), 0.7f);
            DOTween.To(() => Spring7Vector, x => Spring7Vector = x, new Vector3(0, 5.6f, 0), 0.7f).OnComplete(OnSpringTurn5);
        }

        private void OnSpringTurn5()
        {
            DOTween.To(() => Spring1Vector, x => Spring1Vector = x, new Vector3(0, 0, 0), 0.8f);
            DOTween.To(() => Spring2Vector, x => Spring2Vector = x, new Vector3(0, 1, 0), 0.8f);
            DOTween.To(() => Spring3Vector, x => Spring3Vector = x, new Vector3(0, 2, 0), 0.8f);
            DOTween.To(() => Spring4Vector, x => Spring4Vector = x, new Vector3(0, 3.15f, 0), 0.8f);
            DOTween.To(() => Spring5Vector, x => Spring5Vector = x, new Vector3(0, 4.15f, 0), 0.8f);
            DOTween.To(() => Spring6Vector, x => Spring6Vector = x, new Vector3(0, 5.3f, 0), 0.8f);
            DOTween.To(() => Spring7Vector, x => Spring7Vector = x, new Vector3(0, 6.3f, 0), 0.8f).OnComplete(OnSpringTurn6);
        }

        private void OnSpringTurn6()
        {
            DOTween.To(() => Spring1Vector, x => Spring1Vector = x, new Vector3(0, 0, 0), 0.9f);
            DOTween.To(() => Spring2Vector, x => Spring2Vector = x, new Vector3(0, 1, 0), 0.9f);
            DOTween.To(() => Spring3Vector, x => Spring3Vector = x, new Vector3(0, 2, 0), 0.9f);
            DOTween.To(() => Spring4Vector, x => Spring4Vector = x, new Vector3(0, 2.9f, 0), 0.9f);
            DOTween.To(() => Spring5Vector, x => Spring5Vector = x, new Vector3(0, 3.9f, 0), 0.9f);
            DOTween.To(() => Spring6Vector, x => Spring6Vector = x, new Vector3(0, 4.8f, 0), 0.9f);
            DOTween.To(() => Spring7Vector, x => Spring7Vector = x, new Vector3(0, 5.9f, 0), 0.9f).OnComplete(OnSpringTurn7);
        }

        private void OnSpringTurn7()
        {
            DOTween.To(() => Spring1Vector, x => Spring1Vector = x, new Vector3(0, 0, 0), 1);
            DOTween.To(() => Spring2Vector, x => Spring2Vector = x, new Vector3(0, 1, 0), 1);
            DOTween.To(() => Spring3Vector, x => Spring3Vector = x, new Vector3(0, 2, 0), 1);
            DOTween.To(() => Spring4Vector, x => Spring4Vector = x, new Vector3(0, 3, 0), 1);
            DOTween.To(() => Spring5Vector, x => Spring5Vector = x, new Vector3(0, 4, 0), 1);
            DOTween.To(() => Spring6Vector, x => Spring6Vector = x, new Vector3(0, 5, 0), 1);
            DOTween.To(() => Spring7Vector, x => Spring7Vector = x, new Vector3(0, 6, 0), 1);
        }
    }
}
